package com.zhongxi.guoyuan.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.user.ApplyRecordResult;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/1/18.
 */

public class ApplyRecordAdapter extends BaseAdapter {

    private Context mContext;
    private List<ApplyRecordResult.ApplyRecordData> list;
    private String phone;
    private int index = 1;

    public ApplyRecordAdapter(Context context, ArrayList<ApplyRecordResult.ApplyRecordData> data, int index) {
        this.mContext = context;
        this.list = data;
        this.index = index;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            holder = new ViewHolder();
            if(index == 1){
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_order, parent, false);
                holder.tvMoney = (TextView) convertView.findViewById(R.id.tvMoney);
                holder.tvTime = (TextView) convertView.findViewById(R.id.tvApplyTime);
                holder.tvPhone = (TextView) convertView.findViewById(R.id.tvPhone);
                holder.ivCall = (ImageView) convertView.findViewById(R.id.ivCall);
                convertView.setTag(holder);
            }else if(index == 2){
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_apply_recorder, parent, false);
                holder.linearBackground = (LinearLayout) convertView.findViewById(R.id.linearBackground);
                holder.tvDate = (TextView) convertView.findViewById(R.id.tvDate);
                holder.tvPrice = (TextView) convertView.findViewById(R.id.tvPrice);
                convertView.setTag(holder);
            }
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        if(index == 1) {
            if (null != list.get(position) && !"".equals(list.get(position))) {
                ApplyRecordResult.ApplyRecordData data = list.get(position);
                if (null != data.getAmount() && !"".equals(data.getAmount())) {
                    holder.tvMoney.setText("￥" + data.getAmount());
                } else {
                    holder.tvMoney.setText("￥*");
                }
                if (null != data.getCtime() && !"".equals(data.getCtime())) {
                    holder.tvTime.setText("申请时间：" + data.getCtime());
                } else {
                    holder.tvTime.setText("申请时间：*");
                }
            }
            if(null!=phone&&!"".equals(phone)){
                holder.tvPhone.setText("咨询电话："+phone);
                holder.ivCall.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(Intent.ACTION_DIAL);
                        Uri data = Uri.parse("tel:" + phone);
                        intent.setData(data);
                        mContext.startActivity(intent);
                    }
                });
            }
        }else if(index == 2){
            if(position%2==0){
                holder.linearBackground.setBackgroundColor(mContext.getResources().getColor(R.color.bg_box_two));
            }else{
                holder.linearBackground.setBackgroundColor(mContext.getResources().getColor(R.color.bg_box_one));
            }
            if(null!=list.get(position)&&!"".equals(list.get(position))){
                ApplyRecordResult.ApplyRecordData data = list.get(position);
                if(null!=data.getCtime()&&!"".equals(data.getCtime())){
                    holder.tvDate.setText(data.getCtime());
                }else{
                    holder.tvDate.setText("****");
                }
                if(null!=data.getAmount()&&!"".equals(data.getAmount())){
                    holder.tvPrice.setText(data.getAmount());
                }else{
                    holder.tvPrice.setText("**");
                }
            }
        }
        return convertView;
    }

    class ViewHolder{
        private TextView tvMoney;
        private TextView tvTime;
        private TextView tvPhone;
        private ImageView ivCall;
        private LinearLayout linearBackground;
        private TextView tvDate;
        private TextView tvPrice;
     }
}
